<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<title>售票页面</title>
<link rel="stylesheet" href="${ctx}/static/plug/weui/css/weui.css"
	media="all">

<link rel="stylesheet" href="${ctx}/static/plug/weui/css/weuix.css"
	media="all">
	<link rel="stylesheet" href="${ctx}/static/css/sellticket/index.css"
	media="all">
	
<style type="text/css">
</style>



</head>
<body>
	<div class="header">
		<p>综合售票页面
		</p>
		<div class="datetime">
			<span class="times"></span> <span>${user.entNo}，你好</span>
		</div>
		<div class="tips">
			 <span>当前设备连接失败</span>
		</div>
	</div>
	<input type="hidden" value="${user.scenicSpotId}" id="scenicSpotId">
	<div class="subjects">
		<div><span id="sell">售票</span><span id="collection">取票</span><span id="refund">退票</span></div>
		<div><span id="record">售票记录</span><span id="tquery">门票查询</span><span id="statistics">当班统计</span></div>
	</div>
	
	<div class="tailer">
		<div class="Initialization">
			<p>初始化设备</p>
		</div>
	
		<div class="returns">
			<p>退出登录</p>
		</div>
	</div>
	
</body>












<script src="${ctx}/static/js/jquery/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/static/plug/layer/2.4/layer.js"></script>

<script src="${ctx}/static/plug/weui/js/zepto.weui.js"
	type="text/javascript"></script>


<script>
var deviceSubmission = false;
	$(function() {
		$(".Initialization p").click(function() {
			$.login({
                title: '设备连接',
                text: '',
                username: '',  // 默认用户名
                password: '',  // 默认密码
                onOK: function (username, password) {
               	 var serverIP = username;
               	 var serverPort = password;
               	 var host = "ws://" + serverIP + ":" + serverPort + "/"
                    socket = new WebSocket(host);
               	 try {

                        socket.onopen = function (msg) {
                            localStorage.serverIP = serverIP;
                            localStorage.serverPort = serverPort;
                            $(".tips").css("display","none")
                            deviceSubmission = true;
                        };

                        socket.onmessage = function (msg) {
                            if (typeof msg.data == "string") {
                           	 console.log(msg.data)
                        		 var data = $.parseJSON(msg.data);
                        		 console.log(data);
                        		 var deviceId = data.device_id;
                        		 
                            }
                            else {
                                alert("非文本消息");
                            }
                        };

                        socket.onclose = function (msg) { 
                        	alert("socket closed!"); 
                        	$("btnConnect").disabled = false;
                        	$(".tips").css("display","block")
                        	 $.confirm("连接设备失败是否继续连接", "提示", function() {
                        		 webSocketConnect();
                             }, function() {
                                 //取消操作
                                 $(".tips").css("display","block")
                            	 deviceSubmission = false;
                             });
                        };
                    }
                    catch (ex) {
                        log(ex);
                        webSocketConnect();
                        $(".tips").css("display","block")
                        deviceSubmission = false;
                    }
                },
                onCancel: function () {
                	 window.location.reload();
                	$(".tips").css("display","block")
                	deviceSubmission = false;
                }
            });
		});
		
		webSocketConnect();
		function webSocketConnect() {
			if(localStorage.serverIP == null || localStorage.serverPort == null){
				 $.login({
	                 title: '设备连接',
	                 text: '',
	                 username: '',  // 默认用户名
	                 password: '',  // 默认密码
	                 onOK: function (username, password) {
	                	 var serverIP = username;
	                	 var serverPort = password;
	                	 var host = "ws://" + serverIP + ":" + serverPort + "/"
	                     socket = new WebSocket(host);
	                	 try {

	                         socket.onopen = function (msg) {
	                             $.alert("连接成功！","标题",function(){});
	                             $(".tips").css("display","none")
	                             localStorage.serverIP = serverIP;
	                             localStorage.serverPort = serverPort;
	                             deviceSubmission = true;
	                         };

	                         socket.onmessage = function (msg) {
	                             if (typeof msg.data == "string") {
	                            	 console.log(msg.data)
	                         		 var data = $.parseJSON(msg.data);
	                         		 console.log(data);
	                         		 var deviceId = data.device_id;
	                         		 
	                             }
	                             else {
	                                 alert("非文本消息");
	                             }
	                         };

	                         socket.onclose = function (msg) { 
	                        	 $.confirm("连接设备失败是否继续连接", "提示", function() {
	                        		 $(".tips").css("display","block");
	                        		 webSocketConnect();
	                             }, function() {
	                                 //取消操作
	                            	 $(".tips").css("display","block");
	                            	 deviceSubmission = false;
	                             });
	                         };
	                     }
	                     catch (ex) {
	                         log(ex);
	                         webSocketConnect();
	                         $(".tips").css("display","block");
	                         deviceSubmission = false;
	                     }
	                 },
	                 onCancel: function () {
	                	 $(".tips").css("display","block");
	                	 deviceSubmission = false;
	                 }
	             });
			}else {
				deviceSubmission = true;
			}
		}
		
		
		$("#sell").click(function() {
			if(!deviceSubmission){
				$.toast("设备还未未连接成功无法使用", "forbidden");
				return;
			}
			var scenicSpotId = $("#scenicSpotId").val();
			window.location.href="${ctx}/selling/toVoteTicket?scenicSpotId=" + scenicSpotId;
		});
		$("#collection").click(function() {
			if(!deviceSubmission){
				$.toast("设备还未未连接成功无法使用", "forbidden");
				return;
			}
			window.location.href="${ctx}/takeOrRetreat/toCheckCodePick?modeType=" + 1;
		});
		$("#refund").click(function() {
			window.location.href="${ctx}/takeOrRetreat/toCheckCodePick?modeType="  + 2;
		});
		$("#record").click(function() {
			window.location.href="${ctx}/order/index";
		});
		$("#tquery").click(function() {
			window.location.href="${ctx}/order/detailIndex";
		});
		$("#statistics").click(function() {
			window.location.href="${ctx}/order/statistics";
		});

		$(".returns").click(function() {
			 $.confirm("是否退出登录", "提示", function() {
				 window.location.href="${ctx}/index/exit";
             }, function() {
                 //取消操作
             });
		});
		
		getNowFormatDate();
	});
	function getNowFormatDate() {
	    var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    var secondStr = date.getSeconds();
		var minuteStr = date.getMinutes();
		var hourStr = date.getHours();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    if (secondStr >= 0 && secondStr <= 9) {
			secondStr = "0" + secondStr;
		}
		if (minuteStr >= 0 && minuteStr <= 9) {
			minuteStr = "0" + minuteStr;
		}
		if (hourStr >= 0 && hourStr <= 9) {
			hourStr = "0" + hourStr;
		}
		
		var currentdate = date.getFullYear() + seperator1 + month + seperator1
		+ strDate + " " + hourStr + seperator2 + minuteStr
		+ seperator2 + secondStr;
	    $(".times").text(currentdate);
	    setTimeout(getNowFormatDate, 1000);
	}
</script>


</html>